<template>
    <el-dialog :title="!dataForm.pkValue ? '新增' : '修改'" destroy-on-close :before-close="closeDailog"
        :close-on-click-modal="false" :visible.sync="visible" append-to-body>
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
            label-width="80px">
            <el-form-item label="字典值" prop="dictValue">
                <el-input clearable v-model="dataForm.dictValue" placeholder="字典值"></el-input>
            </el-form-item>
            <el-form-item label="字典描述" prop="dictDesc">
                <el-input clearable v-model="dataForm.dictDesc" placeholder="字典描述"></el-input>
            </el-form-item>
            <el-form-item label="字典备注" prop="valueNote">
                <el-input clearable v-model="dataForm.valueNote" placeholder="字典备注"></el-input>
            </el-form-item>
            <el-form-item label="字典排序" prop="valueSort">
                <el-input clearable v-model="dataForm.valueSort" placeholder="字典排序"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="closeDailog">取消</el-button>
            <el-button type="primary" :loading="submitLoading" @click="dataFormSubmit()">确定</el-button>
        </span>
    </el-dialog>
</template>

<script>
    import * as dictService from '@/api/bdms/dict'
    export default {
        data() {
            return {
                visible: false,
                submitLoading: false,
                dataForm: {
                    pkValue: 0,
                    pkType: '',
                    dictValue: '',
                    dictDesc: '',
                    valueNote: '',
                    valueSort: '',
                    revision: 0
                },
                dataRule: {
                    pkType: [{
                        required: true,
                        message: '上级分类不能为空',
                        trigger: 'blur'
                    }],
                    dictValue: [{
                        required: true,
                        message: '字典值不能为空',
                        trigger: 'blur'
                    }],
                    dictDesc: [{
                        required: true,
                        message: '字典描述不能为空',
                        trigger: 'blur'
                    }],
                    valueNote: [{
                        required: true,
                        message: '字典备注不能为空',
                        trigger: 'blur'
                    }],
                    valueSort: [{
                        required: true,
                        message: '字典排序不能为空',
                        trigger: 'blur'
                    }],
                }
            }
        },
        watch:{
            dataForm:{
                handler: function(){
                    this.dataForm.valueNote = this.dataForm.dictDesc + this.dataForm.dictValue
                    if (this.$isNum(this.dataForm.dictValue)){
                        this.dataForm.valueSort = this.dataForm.dictValue
                    }
                },
                deep: true
            }
        },
        methods: {
            init(pkType, id) {
                this.dataForm.pkValue = id || 0
                this.dataForm.pkType = pkType || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    if (this.dataForm.pkValue) {
                        dictService.getDictValue(this.dataForm.pkValue).then(data => {
                            this.dataForm.pkType = data.pkType
                            this.dataForm.dictValue = data.dictValue
                            this.dataForm.dictTag = data.dictTag
                            this.dataForm.dictDesc = data.dictDesc
                            this.dataForm.valueNote = data.valueNote
                            this.dataForm.valueSort = data.valueSort
                            this.dataForm.revision = data.revision
                        })
                    }
                })
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.submitLoading = true
                        var params = {
                            'pkValue': this.dataForm.pkValue || undefined,
                            'pkType': this.dataForm.pkType,
                            'dictValue': this.dataForm.dictValue,
                            'dictDesc': this.dataForm.dictDesc,
                            'valueNote': this.dataForm.valueNote,
                            'valueSort': this.dataForm.valueSort,
                            'revision': this.dataForm.revision
                        }
                        var tick = !this.dataForm.pkValue ? dictService.saveDictValue(params) : dictService
                            .updateDictValue(params)
                        tick.then(data => {
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.submitLoading = false
                                    this.closeDailog()
                                    this.$emit('refreshDataList')
                                }
                            })
                        }).catch((res) => {
                            this.submitLoading = false
                            this.closeDailog()
                        })
                    }
                })
            },
            closeDailog() {
                this.$refs['dataForm'].resetFields()
                this.visible = false
            },
        }
    }
</script>